package TCP;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        //目标:实现TCP通信下一发一收,服务端开发
        System.out.println("服务器端启动了...");
        //1.创建服务端,ServerSocket对象,绑定端口号,监听客户端连接
        ServerSocket ss=new ServerSocket(9999);
        //2.调用accept方法,阻塞等待等待客户端连接,一旦客户端连接会返回一个Socket对象
        Socket socket=ss.accept();
        //3.获取输入流,读取客户端发送的数据
        InputStream is=socket.getInputStream();
        //4.把字节流输入流包装成特殊数据输入流
        DataInputStream dis=new DataInputStream(is);
        //5.读取数据
        int id= dis.readInt();
        String msg=dis.readUTF();
        System.out.println("id"+id+"收到的客户端消息msg="+msg);
        //6.客户端的ip,端口
        System.out.println("客户端的Ip="+socket.getInetAddress().getHostAddress());
        System.out.println("客户端的端口="+socket.getPort());

    }
}
